
export default {
  inserted (el, binding) {
    let oDiv = el
    //  左边距最大值
    let maxLeft = document.documentElement.clientWidth - el.clientWidth
    //  上边距最大值
    let maxTop = document.documentElement.clientHeight - el.clientHeight
    let start
    oDiv.addEventListener('touchstart', start = function (e) {

      // e.preventDefault();
      //  鼠标按下，计算当前元素距离可视区的距离
      let disX = e.touches[0].clientX - oDiv.offsetLeft;
      let disY = e.touches[0].clientY - oDiv.offsetTop;
      // let disX = e.clientX - oDiv.offsetLeft
      // let disY = e.clientY - oDiv.offsetTop
      let move;
      document.addEventListener('touchmove', move = function (e) {
        e.preventDefault();
        //  获取到鼠标拖拽后的横向位移(距离父级元素)
        let l = e.touches[0].clientX - disX;
        let t = e.touches[0].clientY - disY;
        //  获取到鼠标拖拽后的纵向位移(距离父级元素)
        // let t = e.clientY - disY

        if (l <= 0) { l = 0 }

        if (t <= 0) { t = 0 }

        if (l >= maxLeft) { l = maxLeft }

        let tmax = maxTop - (document.documentElement.clientWidth*16*3.125/320)
        if (t >= tmax) { t = tmax }
        //     将此时的位置传出去  
        // binding.value({ x: oDiv.style.left, y: oDiv.style.top })
        oDiv.style.left = l + 'px'

        oDiv.style.top = t + 'px'
      }, { passive: false });

      //     松开事件后，移除事件
      let end;
      document.addEventListener('touchend', end = function (e) {
        // e.preventDefault();
        document.removeEventListener('touchmove', move, false)
        document.removeEventListener('touchend', end, false)
        document.removeEventListener('touchstart', start, false)
      }, { passive: false })
    }
      , { passive: false })
  }
}